package edu.osu.cse.c651.cryptochess;

import java.util.ArrayList;

/**
 * The Rook
 * 
 * @author Team Pawned
 * 
 */
public class Rook implements Piece {

	/**
	 * the Color of this Rook
	 */
	private Color color;

	/**
	 * the number of moves this Rook has made
	 */
	private int moveCount;

	/**
	 * Constructor
	 * 
	 * @param color
	 *            the Color of this Rook
	 */
	public Rook(Color color) {
		this.color = color;
		moveCount = 0;
	}

	/**
	 * 
	 */
	public boolean legalMove(int x1, int y1, int x2, int y2) {
		if ((y2 == y1) && (x2 == x1)) {
			return false;
		}
		if (x2 == x1) {
			return true;
		} else if (y2 == y1) {
			return true;
		}
		return false;
	}

	/**
	 * 
	 */
	public Color color() {
		return color;
	}

	/**
	 * 
	 */
	public PieceName name() {
		return PieceName.Rook;
	}

	/**
	 * 
	 */
	public void movesPlusOne() {
		moveCount += 1;
	}

	/**
	 * 
	 */
	public int moveCount() {
		return moveCount;
	}

	/**
	 * 
	 */
	public ArrayList<Coordinate> possibleMoves(ChessBoard board, int x, int y) {
		ArrayList<Coordinate> moves = new ArrayList<Coordinate>();

		// Check how the rook can move in the positive y direction
		for (int i = y + 1; i < 8; i++) {
			// if no piece is there, its a possible move
			if (board.getPiece(x,i) == null) {
				Coordinate move = new Coordinate(x,i);
				moves.add(move);
			}
			// if its an enemy piece, then it can move here, but no further in
			// this direction
			else if (board.getPiece(x,i).color() != this.color()) {
				Coordinate move = new Coordinate(x,i);
				moves.add(move);
				break;
			}
			// otherwise, its a friendly piece and you can't move here
			else
				break;
		}

		// Check how the rook can move in the negative y direction
		for (int i = y - 1; i >= 0; i--) {
			// if no piece is there, its a possible move
			if (board.getPiece(x,i) == null) {
				Coordinate move = new Coordinate(x,i);
				moves.add(move);
			}
			// if its an enemy piece, then it can move here, but no further in
			// this direction
			else if (board.getPiece(x,i).color() != this.color()) {
				Coordinate move = new Coordinate(x,i);
				moves.add(move);
				break;
			}
			// otherwise, its a friendly piece and you can't move here
			else
				break;
		}

		// Check how the rook can move in the positive x direction
		for (int i = x + 1; i < 8; i++) {
			// if no piece is there, its a possible move
			if (board.getPiece(i,y) == null) {
				Coordinate move = new Coordinate(i,y);
				moves.add(move);
			}
			// if its an enemy piece, then it can move here, but no further in
			// this direction
			else if (board.getPiece(i,y).color() != this.color()) {
				Coordinate move = new Coordinate(i,y);
				moves.add(move);
				break;
			}
			// otherwise, its a friendly piece and you can't move here
			else
				break;
		}

		// Check how the rook can move in the negative x direction
		for (int i = x - 1; i >= 0; i--) {
			// if no piece is there, its a possible move
			if (board.getPiece(i,y) == null) {
				Coordinate move = new Coordinate(i,y);
				moves.add(move);
			}
			// if its an enemy piece, then it can move here, but no further in
			// this direction
			else if (board.getPiece(i,y).color() != this.color()) {
				Coordinate move = new Coordinate(i,y);
				moves.add(move);
				break;
			}
			// otherwise, its a friendly piece and you can't move here
			else
				break;
		}

		return moves;
	}

}
